var dataArr = new Array();// 保存数据的数组
var keyword = "";// 所输入的关键字
var textIdName = "";// text对象Id
var hiddenIdName = "";// hidden对象Id
var noDataIsDisplay = 0;// 没有查到数据时是否要显示,默认显示，1为不需要
var textIdVal = "";// text对象要赋予的值
var hiddenIdNameVal = "";// hidden对象要赋予的值
var divIndex = 0;// 动态生成div内容的Id
var minLen = 3;// 从多少个字开始查询
var key;
var tidTime = null;
var pathUrl;
var autoDivName = "";
var dataCount = 0;
var divWidth = 0;
var isShowSeachDiv = false;

function seachDataByKeyword(urlval, keywordVal, textIdNameObj, hiddenIdNameObj, noDataIsDisplayVal, divWidthVal) {
	if( urlval.indexOf('maxlen')!=-1 ){
		minLen = 2;
	}else{
		minLen = 3;
	}
	if ($.trim(keywordVal).length < minLen) {
		$("#autoDiv").html('');
		$("#autoDiv").hide();
		$("#autoDiv").remove();
		$("#" + hiddenIdNameObj).val('');
		return;
	}
	document.onkeydown = keyDown;
	if ((key == 13 || key == 37 || key == 39 || key == 38 || key == 40) && keywordVal != 'getGeo_') {
		return;
	}
	
	pathUrl = urlval;
	keyword = keywordVal;
	textIdName = textIdNameObj;
	hiddenIdName = hiddenIdNameObj;
	divIndex = 0;
	textIdVal = "";
	divWidth = divWidthVal;
	hiddenIdNameVal = "";
	noDataIsDisplay = noDataIsDisplayVal;
	sendAndRepectData();
}
function sendAndRepectData() {
	if (tidTime != null) {
		clearTimeout(tidTime);
	}
	tidTime = setTimeout(function() {
		var dataString = "keyword=" + keyword;
		$.ajax({
			type : "POST",
			url : pathUrl,
			data : dataString,
			dataType : "xml",
			cache : false,
			success : function(XMLHttpRequest) {
				var id = "";
				var value = "";
				var type = "";
				dataArr.length = 0;
				dataCount = $(XMLHttpRequest).find('count').attr("count");
				$(XMLHttpRequest).find("searchObj").each(function() {
					id = $(this).attr("key");
					value = $(this).attr("value");
					type = $(this).attr("type");
					dataArr[dataArr.length] = new Array(value, id, type);
				});
				writeContent();
			},
			error : function(XMLHttpRequest) {
			}
		});
	}, 200);
	// stopFocus();
}
function stopFocus() {// 控制光标
	var txtnameVal = document.getElementById(textIdName);
	if (navigator.appName.indexOf("Explorer") > -1) {
		var r = txtnameVal.createTextRange();
		r.moveStart("character", txtnameVal.value.length);
		r.collapse(true);
		r.select();
	} else {
		txtnameVal.setSelectionRange(txtnameVal.value.length, txtnameVal.value.length);
		txtnameVal.focus();
	}
}
function suggestOver(div_value) {
	div_value.className = "suggest_link_over";
}
function suggestOut(div_value) {
	div_value.className = "suggest_link";
}
function setSearch(hiddenValue, txtValue, txtValue2, txtValue3) {
	var text = document.getElementById(textIdName);
	textIdVal = txtValue;
	var ss = textIdVal.split("|");
	hiddenIdNameVal = hiddenValue;
	$("#" + textIdName).val(ss[0]);
	$("#" + hiddenIdName).val(hiddenIdNameVal);
	$("#autoDiv").remove();
	if (typeof fillLocaleText != 'undefined' && fillLocaleText instanceof Function) {
		fillLocaleText(hiddenIdName, txtValue2);
	}
	if (typeof text.onblur == "function") {
		text.onblur();
	}
}
function writeContent() {
	$("#autoDiv").remove();
	isShowSeachDiv = false;
	var htmlDiv = "<div id='autoDiv' class='search_data'>";
	htmlDiv += "<span style=\"font-size:9pt\">";
	if (dataArr.length != 0) {
		var index = pathUrl.indexOf("portType=");
		var pathUrl2 = pathUrl.substring(index + 9);
		var lastIndex = pathUrl2.indexOf("&");
		var portType = 0;
		if( lastIndex!=-1 ){
			portType = pathUrl2.substring(0,lastIndex);
		}else{
			portType = pathUrl2;
		}
		if( minLen == 3 ){
			if(keyword == 'getGeo_'){
				var placeText = '以下是距离你身边最近的海港';
				if(portType != 0){
					placeText = '以下是距离你身边最近的空港';
				}
				htmlDiv += "<div style=\"color:#666; font-size:9pt;\">&nbsp;&nbsp;"+placeText+"</div>";
			}else if (portType == 0) {
				htmlDiv += "<div style=\"color:#666; font-size:9pt;\">&nbsp;&nbsp;大写地点表示国际海港，小写地点表示城市</div>";
			} else if (portType == 2) {
				htmlDiv += "<div style=\"color:#666; font-size:9pt;\">&nbsp;&nbsp;大写地点表示国际空港，小写地点表示城市</div>";
			}
		}else{
			htmlDiv += "<div style=\"color:#666; font-size:9pt;\">&nbsp;&nbsp;"+keyword+"</div>";
		}

	} else {
		if (noDataIsDisplay == 1) {
			$("#autoDiv").remove();
			return;
		}
		htmlDiv += "<center style=\"color:#666; font-size:9pt;\">&nbsp;&nbsp;&nbsp;Sorry, can not found " + keyword + "</center>";
	}
	htmlDiv += "<hr style=\"border:1px dotted #aaa;margin:0	0 3px 0;width:99%\" size=\"1\">";
	for ( var i = 0; i < dataArr.length; i++) {
		htmlDiv += "<div style=\"text-align: left\" id=\"" + i + "\" hiddenVal=\"" + dataArr[i][1];
		htmlDiv += "\" textVal=\"" + dataArr[i][0] + "\"";
		htmlDiv += "\" pointTypeVal=\"" + dataArr[i][2] + "\"";
		htmlDiv += " onMouseOver=\"javascript:suggestOver(this);\"";
		htmlDiv += " onmouseout=\"javascript:suggestOut(this);\"";
		htmlDiv += " onclick=\"javascript:setSearch('" + dataArr[i][1].toString() + "','" + dataArr[i][0].toString() + "','" + dataArr[i][2].toString() + "');\"";
		if (divIndex == i) {
			textIdVal = dataArr[i][0];
			hiddenIdNameVal = dataArr[i][1];
			htmlDiv += " class=\"suggest_link_over\" ";
		}
		
		if (dataArr[i][2] == 0 || dataArr[i][2] == 1) {
			htmlDiv += " class=\"suggest_link\"><font color=\"#657c0e\">";
		} else {
			htmlDiv += " class=\"suggest_link\"><font color=\"#000000\">";
		}

		var showData = dataArr[i][0].toString();
		var showDataIndex = showData.toLowerCase().indexOf(keyword.toLowerCase());
		if (showDataIndex != -1) {
			showData = showData.substr(0, showDataIndex)
					+ "<b >"
					+ showData.substr(showDataIndex, keyword.length)
					+ "</b>"
					+ showData.substr(showDataIndex + keyword.length, showData.length);
		} else {
			for ( var c = 1; c < keyword.length; c++) {
				var temp = keyword.toLowerCase().substr(0, c) + ","
						+ keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 1)
							+ "</b>"
							+ showData.substr(showDataIndex + 1 + keyword.length, showData.length);
					break;
				} else {
					var signIndex = temp.indexOf(",");
					var newTemp = temp;
					for ( var d = signIndex; d < temp.length - 1; d++) {
						newTemp = temp.substr(0, d + 2)
								+ ","
								+ temp.substr(d + 2, temp.length - signIndex - 2);
						showDataIndex = showData.toLowerCase().indexOf(newTemp.toLowerCase());
						if (showDataIndex != -1) {
							showData = showData.substr(0, showDataIndex)
									+ "<b >"
									+ showData.substr(showDataIndex, keyword.length + 2)
									+ "</b>"
									+ showData.substr(showDataIndex + 2 + keyword.length, showData.length);
							break;
						}
					}
				}
				// alert(dataArr[i][0].toString().replaceAll(",","").replaceAll("
				// ","").replaceAll("(","").replaceAll(")","").replaceAll("-",""));

				var temp1 = keyword.toLowerCase().substr(0, c) + " " + keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp1.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 1)
							+ "</b>"
							+ showData.substr(showDataIndex + 1 + keyword.length, showData.length);
					break;
				} else {
					var signIndex = temp1.indexOf(" ");
					var newTemp = temp1;
					for ( var d = signIndex; d < temp1.length - 1; d++) {
						newTemp = temp1.substr(0, d + 2)
								+ " "
								+ temp1.substr(d + 2, temp1.length - signIndex - 2);
						showDataIndex = showData.toLowerCase().indexOf(newTemp.toLowerCase());
						if (showDataIndex != -1) {
							showData = showData.substr(0, showDataIndex)
									+ "<b >"
									+ showData.substr(showDataIndex, keyword.length + 2)
									+ "</b>"
									+ showData.substr(showDataIndex + 2 + keyword.length, showData.length);
							break;
						}
					}
				}

				var temp2 = keyword.toLowerCase().substr(0, c) + ", " + keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp2.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 2)
							+ "</b>"
							+ showData.substr(showDataIndex + 2 + keyword.length, showData.length);
					break;
				}

				var temp3 = keyword.toLowerCase().substr(0, c) + " (" + keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp3.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 2)
							+ "</b>"
							+ showData.substr(showDataIndex + 2 + keyword.length, showData.length);
					break;
				}

				var temp4 = keyword.toLowerCase().substr(0, c) + "(" + keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp4.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 1)
							+ "</b>"
							+ showData.substr(showDataIndex + 1 + keyword.length, showData.length);
					break;
				}
				
				var temp5 = keyword.toLowerCase().substr(0, c) + " )" + keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp5.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 2)
							+ "</b>"
							+ showData.substr(showDataIndex + 2 + keyword.length, showData.length);
					break;
				}

				var temp6 = keyword.toLowerCase().substr(0, c) + ")" + keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp6.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 1)
							+ "</b>"
							+ showData.substr(showDataIndex + 1 + keyword.length, showData.length);
					break;
				}
				
				var temp7 = keyword.toLowerCase().substr(0, c) + "), " + keyword.toLowerCase().substr(c, keyword.length);
				showDataIndex = showData.toLowerCase().indexOf(temp7.toLowerCase());
				if (showDataIndex != -1) {
					showData = showData.substr(0, showDataIndex)
							+ "<b >"
							+ showData.substr(showDataIndex, keyword.length + 3)
							+ "</b>"
							+ showData.substr(showDataIndex + 3 + keyword.length, showData.length);
					break;
				}
			}
		}

		htmlDiv += showData;
		htmlDiv += "</font></div>";
	}
	if (dataCount > 1) {
		htmlDiv += '<div style="text-align:center;color:#aaa;">... ...</div>';
	}
	autoDivName = "autoDiv";
	$("#" + textIdName).after(htmlDiv);
	var left = $("#" + textIdName).position().left + 4;
	var top = $("#" + textIdName).position().top + 28;
	$("#autoDiv").css("left", left)[0].scrollTop = top;
	$("#autoDiv").css("width", divWidth);
	$("#autoDiv").bgiframe().show();
	
	document.getElementById("autoDiv").onmouseover = function (){
		isShowSeachDiv = true;
	}
	document.getElementById("autoDiv").onmouseout = function (){
		isShowSeachDiv = false;
	}
	$(document).unbind("click.seachDiv");
	$(document).bind("click.seachDiv",function (){
		if(!isShowSeachDiv && $("#autoDiv").length > 0){
			$("#autoDiv").remove();
			isShowSeachDiv = false;
		}
	})
}

function moveMouseUp() {
	if (divIndex == 0) {
		divIndex = dataArr.length - 1;
		if (document.getElementById(divIndex) != "undefined") {
			document.getElementById("0").className = "suggest_link";
			document.getElementById(divIndex).className = "suggest_link_over";
		}
	} else {
		divIndex = divIndex - 1;
		if (document.getElementById(divIndex) != "undefined") {
			document.getElementById(divIndex).className = "suggest_link_over";
			document.getElementById(divIndex + 1).className = "suggest_link";
		}
	}
	textIdVal = document.getElementById(divIndex).getAttribute("textVal");
	hiddenIdNameVal = document.getElementById(divIndex).getAttribute("hiddenVal");
}
function moveMouseDown() {
	if (divIndex < (dataArr.length - 1)) {
		divIndex = divIndex + 1;
		if (document.getElementById(divIndex) != "undefined") {
			document.getElementById(divIndex).className = "suggest_link_over";
			document.getElementById(divIndex - 1).className = "suggest_link";
		}
	} else {
		if (divIndex == (dataArr.length - 1)) {
			divIndex = 0;
			if (document.getElementById(divIndex) != "undefined") {
				document.getElementById(dataArr.length - 1).className = "suggest_link";
				document.getElementById(divIndex).className = "suggest_link_over";
			}
		}
	}
	if (document.getElementById(divIndex) != "undefined") {
		textIdVal = document.getElementById(divIndex).getAttribute("textVal");
		hiddenIdNameVal = document.getElementById(divIndex).getAttribute("hiddenVal");
	}
}
function keyDown(e) {
	var theEvent = window.event || e;
	key = theEvent.keyCode || theEvent.which;
	if (key == 38) {
		if ($("#autoDiv").html() == null)
			return;
		moveMouseUp();
	}
	if (key == 40) {
		if ($("#autoDiv").html() == null)
			return;
		moveMouseDown();
	}

	if (key == 13) {
		if (noDataIsDisplay == 1) {
			return;
		}
		
		if (document.getElementById("autoDiv") == null
				|| document.getElementById("autoDiv").style.display == "none") {
			return;
		}
		 
		if (document.getElementById(divIndex)) {
			textIdVal = document.getElementById(divIndex).getAttribute("textVal");
			hiddenIdNameVal = document.getElementById(divIndex).getAttribute("hiddenVal");
			var pointTypeVal = document.getElementById(divIndex).getAttribute("pointTypeVal");
			$("#" + textIdName).val(textIdVal);
			$("#" + hiddenIdName).val(hiddenIdNameVal);
			$("#autoDiv").remove();
			if (typeof fillLocaleText != 'undefined' && fillLocaleText instanceof Function) {
				fillLocaleText(hiddenIdName, pointTypeVal);
			}
			var text = document.getElementById(textIdName);
			if (typeof text.onblur == "function") {
				text.onblur();
			}
		}
	} else {
		return;
	}
}

